# -*- coding: utf-8 -*-
"""
    @author:sirius
    @time:2017.10.14
"""
import pandas as pd

def read_mdc_battery():
    """
        根据'db_key'字段结合sys文件'battery'并存储到目标文件
        'battery':电量消耗
    """
    sys_data = pd.read_csv('../datasets/source/sys.csv', sep='\t', dtype={'cint': pd.np.float64}, na_values=['\\N'])
    sys_data = sys_data.loc[:, ['battery']]

    rec_data = pd.read_csv('../datasets/md_cache/app_record.csv', sep=',')

    # 分块读取大文件
    # loop = True
    # chunkSize = 100000
    # chunks = []
    # while loop:
    #     try:
    #         chunk = rec_reader.get_chunk(chunkSize)
    #         chunks.append(chunk)
    #     except StopIteration:
    #         print("Iteration is stopped.")
    #         break

    # rec_data = pd.concat(chunks, ignore_index=True)

    # 组合application文件数据、records文件数据和sys文件数据
    # data = pd.merge(rec_data, sys_data, how='left', on='db_key')
    data = pd.concat([rec_data, sys_data], axis=1)
    data.to_csv('../datasets/md_cache/battery_tmp_sys.csv', index=False)

    print('battery_tmp_sys-Finished...')

def read_mdc_network():
    """
        根据'db_key'字段结合sys文件'network'并存储到目标文件
        'network':网络消耗
    """
    battery_data = pd.read_csv('../datasets/md_cache/battery_tmp_sys.csv', sep=',')

    net_data = pd.read_csv('../datasets/source/gsm.csv', sep='\t')
    net_data = net_data.loc[:, ['signaldbm']]

    # 组合application event、电量消耗数据、网络消耗数据
    # data = pd.merge(battery_data, net_data, how='left', on='db_key')
    data = pd.concat([battery_data, net_data], axis=1)
    data.to_csv('../datasets/md_cache/app_sys.csv', index=False)

    print('App_Sys-Finished...')

if __name__ == '__main__':
    read_mdc_network()
